package com.lyn.demo;

import com.alibaba.fastjson.JSONObject;
import com.feizhou.swagger.annotation.Api;
import com.feizhou.swagger.annotation.ApiOperation;
import com.feizhou.swagger.annotation.Param;
import com.feizhou.swagger.annotation.Params;
import com.jfinal.core.Controller;
import com.jfinal.plugin.redis.Cache;
import com.jfinal.plugin.redis.Redis;

import java.util.List;

@Api(tag = "api.v1.redis", description = "测试redis")
public class RedisController extends Controller {

    @ApiOperation(url = "/testRedis", httpMethod = "post", description = "测试向redis中插入数据")
    public void testRedis() {
        Cache myCache = Redis.use("bbs");
        myCache.set("k1", "v1");
        Object obj = myCache.get("k1");
        System.out.println(obj);

        JSONObject jo = new JSONObject();
        jo.put("user", "chenc");
        jo.put("phone", "139");
        myCache.hset("theKey", "01", jo.toJSONString());

        jo = new JSONObject();
        jo.put("user", "aa");
        jo.put("phone", "133");
        myCache.hset("theKey", "02", jo.toJSONString());

        jo = new JSONObject();
        jo.put("user", "bb");
        jo.put("phone", "134");
        myCache.hset("theKey", "03", jo.toJSONString());

        jo = new JSONObject();
        jo.put("user", "cc");
        jo.put("phone", "135");
        myCache.hset("theKey", "04", jo.toJSONString());

        List<String> values = myCache.hmget("theKey", "01", "02", "03", "04");
        for(String value : values) {
            System.out.println(value);
        }

        System.out.println("---------------------------------------");

        Object simpleValue = myCache.get("thekey");
        System.out.println("the key value ="+simpleValue);
        Object v1 = myCache.get("k1");
        System.out.println("k1 value="+v1);


        renderJson();

    }


}
